import { createReducer, createActions } from 'reduxsauce'
import Immutable from 'seamless-immutable'

/* ------------- Types and Action Creators ------------- */

const { Types, Creators } = createActions({
  //callRequest: ['addressId', 'commodityId', 'orderType', 'token'],
  //callRequest: ['commodityId', 'address', 'longitude', 'latitude', 'couponId','token'],
  callRequest: ['data'],
  callSuccess: ['payload'],
  callFailure: null,
})

export const CallTypes = Types
export default Creators

/* ------------- Initial State ------------- */

export const INITIAL_STATE = Immutable({
  //commodityId: null,
  //address: null,
  //longitude: null,
  //latitude: null,
  data: null,
  fetching: null,
  payload: null,
  error: null
})

/* ------------- Reducers ------------- */
// initial
export const initial = state => INITIAL_STATE

// request the data from an api
//export const request = (state, {commodityId,address,longitude,latitude,couponId,token}) =>
//  state.merge({ fetching: true, commodityId,address,longitude,latitude,couponId,payload: null })
export const request = (state, {data}) =>
  state.merge({ fetching: true, data:data, payload: null })

// successful api lookup
export const success = (state, action) => {
  const { payload } = action
  return state.merge({ fetching: false, error: null, payload})
}

// Something went wrong somewhere.
export const failure = state =>
  state.merge({ fetching: false, error: true, payload: null })

/* ------------- Hookup Reducers To Types ------------- */

export const reducer = createReducer(INITIAL_STATE, {
  [Types.CALL_REQUEST]: request,
  [Types.CALL_SUCCESS]: success,
  [Types.CALL_FAILURE]: failure
})
